package com.lancoo.znbkxx.teacher.adapter

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.ChildOption
import com.lancoo.znbkxx.teacher.model.data.Item
import kotlinx.android.synthetic.main.item_choice_wrong.view.*
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @author : wealive
 * @date   : 2020/6/30 9:55
 * @desc   :
 */
class MatcherAdapter(data: MutableList<MultiItemEntity>) :
    BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {
    val TYPE_LEVEL_0 = 0
    val TYPE_WRONG = 1

    init {
        addItemType(TYPE_LEVEL_0, R.layout.item_choice_wrong)
        addItemType(TYPE_WRONG, R.layout.item_choice_wrong)
    }

    override fun convert(helper: BaseViewHolder, item: MultiItemEntity?) {
        with(helper.itemView) {
            when (helper.itemViewType) {
                TYPE_LEVEL_0 -> {
                    val subChildItemX = item as Item
                    val percent =
                        "(${subChildItemX.StuCount}，约${subChildItemX.Rate})"
                    val answer = subChildItemX.ItemAnswer
                    tv_choice.text = "${subChildItemX.SortIndex}.${answer} ${percent}"
                    setOnClickListener {
                        val pos = helper.layoutPosition
                        if (subChildItemX.isExpanded) {
                            collapse(pos)
                        } else {
                            expand(pos)
                        }
                    }
                    if (subChildItemX.isExpanded) {
                        tv_choice.setTextColor(Color.parseColor("#27ddd7"))
                        if (subChildItemX.SortIndex == 1) {
                            setBackgroundResource(R.drawable.bg_matcher_white_first)
                        } else {
                            setBackgroundResource(R.drawable.bg_matcher_white_top)
                        }
                    } else {
                        tv_choice.setTextColor(Color.parseColor("#ffffff"))
                        if (subChildItemX.SortIndex == 1) {
                            setBackgroundResource(R.drawable.bg_matcher_top)
                        } else if (subChildItemX.SortIndex < subChildItemX.Size) {
                            setBackgroundColor(Color.parseColor("#27ddd7"))
                        } else {
                            setBackgroundResource(R.drawable.bg_matcher_bottom)
                        }
                    }
                }
                TYPE_WRONG -> {
                    val optionX = item as ChildOption
                    val percent =
                        "(${optionX.StuCount}，约${optionX.Rate})"
                    val answer = optionX.OptionIndex
                    when {
                        optionX.Index == 0 -> {
                            if (optionX.size == 1) {
                                setBackgroundResource(R.drawable.bg_matcher_white_last)
                            } else {
                                setBackgroundResource(R.drawable.bg_matcher_white_middle)
                            }
                            tv_choice.text = "错误较多：${answer} $percent"
                        }
                        optionX.Index + 1 < optionX.size -> {
                            setBackgroundResource(R.drawable.bg_matcher_white_middle)
                            val span = SpannableStringBuilder("错误较多：${answer} $percent");
                            span.setSpan(
                                ForegroundColorSpan(Color.TRANSPARENT),
                                0,
                                5,
                                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
                            )
                            tv_choice.text = span
                        }
                        else -> {
                            if (optionX.parentIndex == optionX.parentSize) {
                                setBackgroundResource(R.drawable.bg_matcher_white_bottom)
                            } else {
                                setBackgroundResource(R.drawable.bg_matcher_white_last)
                            }
                            val span = SpannableStringBuilder("错误较多：${answer} $percent");
                            span.setSpan(
                                ForegroundColorSpan(Color.TRANSPARENT),
                                0,
                                5,
                                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
                            )
                            tv_choice.text = span
                        }
                    }
                }
            }
        }
    }
}